package com.financing.wap.web.controller.api;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.Session;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.common.BizEnums.CommonEnums;
import com.financing.wap.common.ResultMap.STATUS;
import com.financing.wap.dto.FpFaUserSaysayCommentDTO;
import com.financing.wap.dto.FpFaUserSaysayDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpFaUserSaysayCommentService;
import com.financing.wap.service.IFpFaUserSaysayService;
import com.financing.wap.service.IUserService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.util.PropertiesUtils;
import com.financing.wap.web.controller.BaseController;

@Controller("fpFaUserSaysayController")
@RequestMapping("/api/fpFaUserSaysay")
public class FpFaUserSaysayController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	
	@Autowired
	private IFpFaUserSaysayCommentService fpFaUserSaysayCommentService;
	
	@Autowired
	private IFpFaUserSaysayService fpFaUserSaysayService;
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/listFpFaUserSaysay")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpFaUserSaysayDTO fpFaUserSaysay,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaUserSaysay list begin .......");
			
			String loginMobile=request.getParameter("mobile");
			if(StringUtils.isBlank(loginMobile)){
				//查询当前用户的活动
				UserDTO u=SessionUtil.getSession().getUser();
				if(null == u){
					result.setStatusCode(ResultMap.LOGIN_NOLOGIN_ERROR);
					result.setStatus(ResultMap.STATUS.FAILE);
					return result;
				}
				//查询当前用户的说说
				fpFaUserSaysay.setUserId(u.getId());
			}else{
				UserDTO dto=new UserDTO();
				dto.setLoginName(loginMobile);
				UserDTO u = userService.queryUserLoginName(dto);
				fpFaUserSaysay.setUserId(u.getId());
			}
			
			//查询当前用户的说说
//			fpFaUserSaysay.setUserId(loginUser.getId());
			
			fpFaUserSaysay.setBegin(pager.getStartIndex());
			fpFaUserSaysay.setPageSize(pager.getPageSize());
			
			fpFaUserSaysay.setDeleteTag("1");
			
			int count = fpFaUserSaysayService.queryListCount(fpFaUserSaysay);
			
			pager.setTotalCount(count);
			
			List<FpFaUserSaysayDTO> fpFaUserSaysayList = null;
			
			if(count > 0){
				fpFaUserSaysayList = fpFaUserSaysayService.queryList(fpFaUserSaysay);
				for (FpFaUserSaysayDTO saysayDto:fpFaUserSaysayList) {
					 FpFaUserSaysayCommentDTO fpFaUserSaysayComment=new FpFaUserSaysayCommentDTO();
					 fpFaUserSaysayComment.setSaysayId(saysayDto.getId());
					fpFaUserSaysayComment.setDeleteTag(CommonEnums.DELETE_FLAG_1);
					List<FpFaUserSaysayCommentDTO> fpFaUserSaysayCommentList = fpFaUserSaysayCommentService.queryList(fpFaUserSaysayComment);
					for (FpFaUserSaysayCommentDTO saysayCommentDto:fpFaUserSaysayCommentList) {
						String commentUserId = saysayCommentDto.getCommentUserId();
						if(commentUserId!=null){
							UserDTO u = userService.queryUserById(commentUserId);
							saysayCommentDto.setUserName(u.getUserName());
							saysayCommentDto.setUserHeadImg(u.getuImage());
						}else{
							saysayCommentDto.setUserName("游客");
							try{//去第一张默认头像
								String[] filesName = PropertiesUtils.getValue("faUserImageFile").split(";");
								saysayCommentDto.setUserHeadImg(PropertiesUtils.getValue("faUserImagePath")+filesName[0]);
							}catch(Exception e){
								Log.warn("create fa user image error",e);
							}
						}
					}
					saysayDto.setSaysayCommentList(fpFaUserSaysayCommentList);
					saysayDto.setGetList(new ArrayList());
				}
			}else{
				fpFaUserSaysayList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			
			//按年分组
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			TreeMap map = new TreeMap();
			String years="";
			ArrayList list = new ArrayList();
			for (int i = 0; i < fpFaUserSaysayList.size(); i++) {
				if(i==0){
					years=sdf.format(fpFaUserSaysayList.get(i).getCreateDate()).substring(0, 4);
				}
				if(!years.equals(sdf.format(fpFaUserSaysayList.get(i).getCreateDate()).substring(0, 4))){
					if(i!=0){
						map.put((1000+i)+years, list);
						list=new ArrayList();
					}
					years=sdf.format(fpFaUserSaysayList.get(i).getCreateDate()).substring(0, 4);
				}
				list.add(fpFaUserSaysayList.get(i));
				if((i+1)==fpFaUserSaysayList.size()){
					map.put((1001+i)+years, list);
				}
			}
			
			result.setAttribute("list", fpFaUserSaysayList);
			result.setAttribute("years", map);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaUserSaysay list end .......");
		}catch(Exception e){
			log.error("query fpFaUserSaysay error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpFaUserSaysay")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpFaUserSaysayDTO fpFaUserSaysay){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpFaUserSaysay  begin .......");
			
			if(StringUtils.isBlank(fpFaUserSaysay.getSContent()) && StringUtils.isBlank(fpFaUserSaysay.getSImages())){
				result.setStatus(STATUS.ERROR);
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setMessage("参数缺失！");
				return result;
			}
			Session session = getSession(true);
			UserDTO loginUser = session.getUser();
			String faId = loginUser.getId();
			fpFaUserSaysay.setUserId(faId);
			DTOdateSetUtil.setCreateInfo(fpFaUserSaysay);
			DTOdateSetUtil.setUpdateInfo(fpFaUserSaysay);
			fpFaUserSaysay.setDeleteTag("1");
			
			int id = fpFaUserSaysayService.insert(fpFaUserSaysay);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpFaUserSaysay list end .......");
		}catch(Exception e){
			log.error("add fpFaUserSaysay error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpFaUserSaysay")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpFaUserSaysayDTO fpFaUserSaysay){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpFaUserSaysay  begin .......");
			
			Integer id = fpFaUserSaysay.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpFaUserSaysayDTO dto = fpFaUserSaysayService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpFaUserSaysay  end .......");
		}catch(Exception e){
			log.error("query detail fpFaUserSaysay error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpFaUserSaysay")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpFaUserSaysayDTO fpFaUserSaysay){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpFaUserSaysay  begin .......");
			
			Integer id = fpFaUserSaysay.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpFaUserSaysayService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpFaUserSaysay  end .......");
		}catch(Exception e){
			log.error("delete fpFaUserSaysay error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpFaUserSaysay")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpFaUserSaysayDTO fpFaUserSaysay){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpFaUserSaysay  begin .......");
			
			Integer id = fpFaUserSaysay.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpFaUserSaysay);
			
			fpFaUserSaysayService.update(fpFaUserSaysay);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpFaUserSaysay list end .......");
		}catch(Exception e){
			log.error("update fpFaUserSaysay error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpFaUserSaysay")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpFaUserSaysayDTO fpFaUserSaysay){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpFaUserSaysay  begin .......");
			
			Integer id = fpFaUserSaysay.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpFaUserSaysay);
			
			fpFaUserSaysayService.updateForce(fpFaUserSaysay);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpFaUserSaysay list end .......");
		}catch(Exception e){
			log.error("updateForce fpFaUserSaysay error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
